package com.my_test.webmagic.webmagic_modules.controller;

import com.alibaba.fastjson.JSONObject;
import com.my_test.webmagic.module.entity.SearchWord;
import com.my_test.webmagic.module.service.SearchUserService;
import com.my_test.webmagic.module.service.SearchWordService;
import com.my_test.webmagic.webmagic_modules.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.RedisScheduler;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.sql.DatabaseMetaData;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangyalin@act-telecom.com
 * @date 2019-06-05 11:42
 */
@Controller
@RequestMapping("webmagic")
public class WebMagicController {

    @Resource
    private SearchWebMagicProcessor searchWebMagicProcessor;
    @Resource
    private SearchWebMagicMysqlPipeline searchWebMagicMysqlPipeline;
    @Resource
    private SearchRedisScheduler searchRedisScheduler;
    @Resource
    private SearchWordService searchWordService;


    @GetMapping("keyword")
    @ResponseBody
    public Map<String, Object> keyword(@RequestParam String keyword) {
        SearchWord word = new SearchWord();
        word.setWord(keyword);
        word.setSerchTime(new Date());
        searchWordService.save(word);
        try {
            String encode = URLEncoder.encode(keyword, "utf8");
            Spider.create(searchWebMagicProcessor).setScheduler(searchRedisScheduler).addPipeline(searchWebMagicMysqlPipeline).addUrl("http://tieba.baidu.com/f/search/res?ie=utf-8&qw=" + encode).thread(5).start();
            Spider.create(searchWebMagicProcessor).setScheduler(searchRedisScheduler).addPipeline(searchWebMagicMysqlPipeline).addUrl("http://tieba.baidu.com/f/search/fm?ie=UTF-8&qw=" + encode).thread(5).start();
            Spider.create(searchWebMagicProcessor).setScheduler(searchRedisScheduler).addPipeline(searchWebMagicMysqlPipeline).addUrl("https://www.baidu.com/p/" + encode + "?from=tieba").thread(5).start();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return httpGet(keyword);
    }

    /**
     * https://tieba.baidu.com/suggestion?query=sdf&ie=utf-8&_=1559714299207
     */
    public static Map<String, Object> httpGet(String keyword) {
        String url = "https://tieba.baidu.com/suggestion";
        HttpClient httpClient = HttpClients.createDefault();
        try {
            URIBuilder builder = new URIBuilder(url);
            builder.addParameter("query", keyword);
            builder.addParameter("ie", "utf-8");
            builder.addParameter("_", System.currentTimeMillis() + "");
            HttpGet httpGet = new HttpGet(builder.build());
            HttpResponse response = httpClient.execute(httpGet);
            return JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //query_match query_tag  query_tips  query_operate suggest_game  hottopic_list
    public static void main(String[] args) {
        String s1 = "http://tieba.baidu.com/f/search/res?isnew=1&kw=&qw=%CA%B1%BC%E4&rn=10&un=&only_thread=0&sm=1&sd=&ed=&pn=6";
        String s2 = "http://tieba.baidu.com/f/search/fm?kw=&qw=%CA%B1%BC%E4&rn=10&un=&sm=&sd=&ed=&pn=3";
        String s3 = "https://www.baidu.com/p/%CA%B1%BC%E4?from=tieba";
        System.out.println(s1.matches("http[s]?://tieba.baidu.com/f/search/res\\?[\\S]*&qw=[\\S]+"));
        System.out.println(s2.matches("http[s]?://tieba.baidu.com/f/search/fm\\?[\\S]*&qw=[\\S]+"));
        System.out.println(s3.matches("http[s]?://www.baidu.com/p/[\\S]+\\?from=tieba"));
    }

}
